﻿using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DLV.BackEnd.Pages
{
    public partial class EditPrice : AdminPageBase
    {
        protected int Type = 0;
        private PriceInfo _priceInfo = new PriceInfo();
        private readonly PriceController _priceController = new PriceController();

        private TicketInfo _ticketInfo = new TicketInfo();
        private readonly TicketController _ticketController = new TicketController();

        private InsuranceInfo _insuranceInfo = new InsuranceInfo();
        private readonly InsuranceController _insuranceController = new InsuranceController();

        private ServiceInfo _serviceInfo = new ServiceInfo();
        private readonly ServiceController _serviceController = new ServiceController();

        private RoomInfo _roomInfo = new RoomInfo();
        private readonly RoomController _roomController = new RoomController();
        private MenuInfo _menuInfo = new MenuInfo();
        private readonly MenuController _menuController = new MenuController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["Type"]))
            {
                Type = int.Parse(Request.QueryString["Type"]);
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PriceId"]))
            {
                _priceInfo = _priceController.GetPrice(int.Parse(Request.QueryString["PriceId"]));               
                
                if (Type == 1 || Type == 2 || Type == 3)
                {
                    _ticketInfo = _ticketController.GetTicket(_priceInfo.ItemId);
                }

                if (Type == 4)
                {
                    _menuInfo = _menuController.GetMenu(_priceInfo.ItemId);
                }
                if (Type == 5)
                {
                    _roomInfo = _roomController.GetRoom(_priceInfo.ItemId);
                }
                if (Type == 7)
                {
                    _insuranceInfo = _insuranceController.GetInsurance(_priceInfo.ItemId);
                }
                if (Type == 8)
                {
                    _serviceInfo = _serviceController.GetService(_priceInfo.ItemId);
                }
            }
           
            if (Page.IsPostBack) return;
             
                if(Type==1||Type==2||Type==3)
                {
                    LoadConcern();
                    LoadRatedByType();
                    LoadSoCho();
                    LoadFlightArrivals();
                }
                if(Type==4)
                {
                    LoadRestaurant();
                    LoadMenuType();
                }
                if (Type == 5)
                {
                    LoadHotel();
                }
                if(Type==7)
                {
                    LoadPartner();
                }
                if (Type == 8)
                {
                    
                    LoadLocationType();
                    LoadLocation();
                }
                LoadTicketType();
                LoadSeason();
                LoadNational();
                LoadAge();
                LoadUser();
                LoadStatus();
                AddValueDefaultForDropdown();
            
            if (_priceInfo.PriceId == -1) return;
                if (Type == 1 || Type == 2 || Type == 3)
                {
                    LoadTicketByConcern();
                }
                if(Type==4)
                {
                    LoadMenuByRestaurantAndMenuType();
                }
                if (Type == 5)
                {
                    LoadRoomByHotel();
                }
                if (Type == 7)
                {
                    LoadInsuranceByPartner();
                }
                if (Type == 8)
                {
                    LoadPartnerService();
                    LoadServiceByLocation();
                    LoadLocationByLocationType();
                    LoadCityService();
                }   
                RenderForm();
        }
        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            RedirectLink();
        }
        
        private void SaveData()
        {
            _priceInfo.ItemId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlTicket"]);
            _priceInfo.ItemType = Type;          
            _priceInfo.AgeId= int.Parse(ddlAge.SelectedValue);
            
            _priceInfo.Time = txtTime.Text;
            
            if (Type == 3)
            {
                _priceInfo.Time = ddlSoCho.Text;
            }
            _priceInfo.NationalId = int.Parse(ddlNational.SelectedValue);
            _priceInfo.SalePrice = decimal.Parse(txtSalePrice.Text);
            _priceInfo.BeginDate = DateTime.Parse(dteBeginDate.Value);
            _priceInfo.EndDate = DateTime.Parse(dteEndDate.Value);
            _priceInfo.FlightArrivals = ddlFlightArrivals.Text;

            _priceInfo.SalePriceChild = decimal.Parse(txtSalePriceChild.Text);
            if (Type == 1 && Type==2 && Type==3)

            {
                _priceInfo.Rated = int.Parse(ddlRated.SelectedValue);
            }
            _priceInfo.TicketType = int.Parse(ddlTicketType.SelectedValue);
            _priceInfo.Season = ddlSeason.Text;

            _priceInfo.Status = int.Parse(ddlStatus.SelectedValue);



            if (_priceInfo.PriceId == -1)
            {
                _priceInfo.CreatedBy = ddlCreatedBy.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlCreatedBy.SelectedValue);
                _priceInfo.CreatedDate = DateTime.Now;
                _priceController.InsertPrice(_priceInfo);
            }
            else
            {
                _priceInfo.UpdatedBy = UserId;
                _priceInfo.UpdatedDate = DateTime.Now;
                _priceController.UpdatePrice(_priceInfo);
            }

            RedirectLink();
        }
          
        private void RenderForm()
        {
            if(Type==5)
            { 
                ddlConcern.SelectedValue = HotelId(_priceInfo.ItemId);
            }
            if (Type == 1 || Type == 2 || Type == 3)
            {
                ddlConcern.SelectedValue = _ticketInfo.ConcernId.ToString();
            }
            if (Type == 4)
            {
                ddlConcern.SelectedValue = _menuInfo.RestaurantId.ToString();
                ddlMenuType.SelectedValue = _menuInfo.MenuTypeId.ToString();
            }
            if (Type == 7)
            {
                ddlConcern.SelectedValue = _insuranceInfo.PartnerId.ToString();
            }
            if (Type == 8)
            {
                int locationTypeId = new LocationController().GetLocation(_serviceInfo.LocationId).LocationTypeId;
                int nationalId =new PartnerController().GetPartner(_serviceInfo.PartnerId).NationalId;
                int cityId = new LocationController().GetLocation(_serviceInfo.LocationId).CityId;
                ddlLocationType.SelectedValue = locationTypeId.ToString();
                ddlConcern.SelectedValue = _serviceInfo.PartnerId.ToString();
                ddlLocation.SelectedValue = _serviceInfo.LocationId.ToString();
                ddlNationalService.SelectedValue = nationalId.ToString();
                ddlCity.SelectedValue = cityId.ToString();

            }
            ddlTicket.SelectedValue = _priceInfo.ItemId.ToString();
            ddlAge.SelectedValue = _priceInfo.AgeId.ToString();
            txtTime.Text = _priceInfo.Time;
            ddlSoCho.SelectedValue = _priceInfo.Time;
            ddlNational.SelectedValue = _priceInfo.NationalId.ToString();
            txtSalePrice.Text = _priceInfo.SalePrice.ToString();
            ddlCreatedBy.SelectedValue = _priceInfo.CreatedBy.ToString();
            ddlStatus.SelectedValue = _priceInfo.Status.ToString();
            ddlRated.SelectedValue = _priceInfo.Rated.ToString();
            ddlFlightArrivals.Text = _priceInfo.FlightArrivals;
            ddlTicketType.SelectedValue = _priceInfo.TicketType.ToString();
            dteBeginDate.Value = _priceInfo.BeginDate.ToShortDateString();
            dteEndDate.Value = _priceInfo.EndDate.ToShortDateString();
            ddlSeason.Text = _priceInfo.Season;
            txtSalePriceChild.Text = _priceInfo.SalePriceChild.ToString();
            
        }
          
        
        private void RedirectLink()
        {
            var link = String.Empty;
            switch (Type)
            {
                case 1:
                    link = "/Pages/ListPrice.aspx?xml=PriceAir&Type=" + Type;
                    break;
                case 2:
                    link = "/Pages/ListPrice.aspx?xml=PriceStation&Type=" + Type;
                    break;
                case 3:
                    link = "/Pages/ListPrice.aspx?xml=PriceCar&Type=" + Type;
                    break;
                case 7:
                    link = "/Pages/ListPrice.aspx?xml=PriceInsurance&Type=" + Type;
                    break;
                case 8:
                    link = "/Pages/ListPrice.aspx?xml=PriceService&Type=" + Type;
                    break;
                case 5:
                    link = "/Pages/ListPrice.aspx?xml=PriceHotel&Type=" + Type;
                    break;
                case 4:
                    link = "/Pages/ListPrice.aspx?xml=PriceMenu&Type=" + Type;
                    break;
            }
            Response.Redirect(link);
        }

        private string HotelId(int roomId)
        {
            var hotelInfo = new HotelController().GetHotelByRoom(roomId);
            return hotelInfo.HotelId.ToString();
        }
        private void LoadConcern()
        {
            ddlConcern.DataSource = new ConcernController().ListConcernByStatus((int)Globals.DocumentStatus.Use).Where(p=>p.ConcernType==Type).OrderBy(p=>p.ConcernFullName);
            ddlConcern.DataTextField = "ConcernFullName";
            ddlConcern.DataValueField = "ConcernId";
            ddlConcern.DataBind();
            if (Type != 3)
            {
                ddlConcern.Items.Insert(0, new ListItem("--- Chọn hãng---", "-1"));
            }
            else { ddlConcern.Items.Insert(0, new ListItem("--- Chọn hiệu xe---", "-1")); }
        }
     
        private void LoadRestaurant()
        {
            ddlConcern.DataSource = new RestaurantController().ListRestaurantByStatus((int)Globals.DocumentStatus.Use).OrderBy(p => p.RestaurantFullName);
            ddlConcern.DataTextField = "RestaurantFullName";
            ddlConcern.DataValueField = "RestaurantId";
            ddlConcern.DataBind();

            ddlConcern.Items.Insert(0, new ListItem("--- Chọn nhà hàng---", "-1"));
        }
        private void LoadMenuType()
        {
            ddlMenuType.DataSource = new MenuTypeController().ListMenuType().Where(p => p.Status == (int)Globals.DocumentStatus.Use).OrderBy(p => p.MenuTypeName);
            ddlMenuType.DataTextField = "MenuTypeName";
            ddlMenuType.DataValueField = "MenuTypeId";
            ddlMenuType.DataBind();

            ddlMenuType.Items.Insert(0, new ListItem("--- Chọn bữa ăn---", "-1"));
        }
        private void LoadHotel()
        {
            ddlConcern.DataSource = new HotelController().ListHotel().Where(p=>p.Status==(int)Globals.DocumentStatus.Use).OrderBy(p => p.HotelFullName);
            ddlConcern.DataTextField = "HotelFullName";
            ddlConcern.DataValueField = "HotelId";
            ddlConcern.DataBind();

            ddlConcern.Items.Insert(0, new ListItem("--- Chọn khách sạn---", "-1"));
        }
        private void LoadPartner()
        {
            ddlConcern.DataSource = new PartnerController().ListPartnerByStatus((int)Globals.DocumentStatus.Use).OrderBy(p=>p.PartnerName);
            ddlConcern.DataTextField = "PartnerName";
            ddlConcern.DataValueField = "PartnerId";
            ddlConcern.DataBind();

            ddlConcern.Items.Insert(0, new ListItem("--- Chọn nhà cung cấp---", "-1"));
        }
        private void LoadPartnerService()
        {
            var nationalId = new LocationController().GetLocation(_serviceInfo.LocationId).NationalId;
            ddlConcern.DataSource = new PartnerController().ListPartnerByStatus((int)Globals.DocumentStatus.Use).Where(p=>p.NationalId==nationalId).OrderBy(p => p.PartnerName);
            ddlConcern.DataTextField = "PartnerName";
            ddlConcern.DataValueField = "PartnerId";
            ddlConcern.DataBind();

            ddlConcern.Items.Insert(0, new ListItem("--- Chọn nhà cung cấp---", "-1"));
        }
        private void LoadCityService()
        {
            var nationalId = new LocationController().GetLocation(_serviceInfo.LocationId).NationalId;
            ddlCity.DataSource = new CityController().ListCityByNational(nationalId).OrderBy(p=>p.CityName);
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityId";
            ddlCity.DataBind();

            ddlCity.Items.Insert(0, new ListItem("--- Chọn tỉnh/thành phố---", "-1"));
        }
        private void LoadLocation()
        {
            //ddlLocation.DataSource = new LocationController().ListLocationByStatus((int)Globals.DocumentStatus.Use).OrderBy(p => p.LocationName);
            //ddlLocation.DataTextField = "LocationName";
            //ddlLocation.DataValueField = "LocationId";
            //ddlLocation.DataBind();

            ddlLocation.Items.Insert(0, new ListItem("--- Chọn địa điểm---", "-1"));
        }
        private void LoadLocationByLocationType()
        {
            var locationTypeId = new LocationController().GetLocation(_serviceInfo.LocationId).LocationTypeId;
            ddlLocation.DataSource = new LocationController().ListLocationByLocationType(locationTypeId).OrderBy(p => p.LocationName);
            ddlLocation.DataTextField = "LocationName";
            ddlLocation.DataValueField = "LocationId";
            ddlLocation.DataBind();

            ddlLocation.Items.Insert(0, new ListItem("--- Chọn địa điểm---", "-1"));
        }
        private void LoadLocationType()
        {
            ddlLocationType.DataSource = new LocationTypeController().ListLocationType().Where(p=>p.Status==(int)Globals.DocumentStatus.Use).OrderBy(p => p.LocationTypeName);
            ddlLocationType.DataTextField = "LocationTypeName";
            ddlLocationType.DataValueField = "LocationTypeId";
            ddlLocationType.DataBind();

            ddlLocationType.Items.Insert(0, new ListItem("--- Chọn loại địa điểm---", "-1"));
        }
        private void LoadNational()
        {
            ddlNational.DataSource = new NationalController().ListNational().OrderBy(p=>p.NationalName);
            ddlNational.DataTextField = "NationalName";
            ddlNational.DataValueField = "NationalId";
            ddlNational.DataBind();

            ddlNational.Items.Insert(0, new ListItem("--- Chọn quốc gia---", "-1"));

            ddlNationalService.DataSource = new NationalController().ListNational().OrderBy(p => p.NationalName);
            ddlNationalService.DataTextField = "NationalName";
            ddlNationalService.DataValueField = "NationalId";
            ddlNationalService.DataBind();

            ddlNationalService.Items.Insert(0, new ListItem("--- Chọn quốc gia---", "-1"));
        }

        private void LoadAge()
        {
            ddlAge.DataSource = new AgeController().ListAge().Where(p => p.Status == (int)Globals.DocumentStatus.Use).OrderBy(p=>p.AgeName);
            ddlAge.DataTextField = "AgeName";
            ddlAge.DataValueField = "AgeId";
            ddlAge.DataBind();

            ddlAge.Items.Insert(0, new ListItem("--- Chọn độ tuổi ---", "-1"));
        }

        private void LoadSeason()
        {
            ddlSeason.Items.Add("Thấp điểm");
            ddlSeason.Items.Add("Cao điểm");
            ddlSeason.Items.Add("Cực cao điểm");

            ddlSeason.Items.Insert(0,new ListItem("---Chọn mùa du lịch---","-1"));
        }
        private void LoadSoCho()
        {
            ddlSoCho.Items.Add("4");
            ddlSoCho.Items.Add("5");
            ddlSoCho.Items.Add("7");
            ddlSoCho.Items.Add("8");
            ddlSoCho.Items.Add("10");
            ddlSoCho.Items.Add("16");
            ddlSoCho.Items.Add("30");
            ddlSoCho.Items.Insert(0, new ListItem("--- Chọn số chỗ ---", "-1"));
        }
        private void AddValueDefaultForDropdown()
        {
            ddlTicket.Items.Insert(0, new ListItem("*", "-1"));
        }
        private void LoadUser()
        {
            ddlCreatedBy.DataSource = new AdminUserController().ListAdminUser();
            ddlCreatedBy.DataTextField = "UserName";
            ddlCreatedBy.DataValueField = "UserId";
            ddlCreatedBy.DataBind();

            ddlCreatedBy.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
        private void LoadRatedByType()
        {
            ddlRated.DataSource = new TypeController().ListTypeByTypeOf(Type);
            ddlRated.DataTextField = "TypeName";
            ddlRated.DataValueField = "TypeId";
            ddlRated.DataBind();

            ddlRated.Items.Insert(0, new ListItem("--- Chọn hạng vé ---", "-1"));
        }

        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
        private void LoadTicketType()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/TicketType.xml"));
            var dv = ds.Tables["TicketType"].DefaultView;
            ddlTicketType.DataTextField = "Name";
            ddlTicketType.DataValueField = "ID";
            ddlTicketType.DataSource = dv;
            ddlTicketType.DataBind();

            ddlTicketType.Items.Insert(0, new ListItem("--- Chọn loại vé ---", "-1"));
        }
        private void LoadFlightArrivals()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/FlightArrivals.xml"));
            var dv = ds.Tables["FlightArrivals"].DefaultView;
            ddlFlightArrivals.DataTextField = "Name";
            ddlFlightArrivals.DataValueField = "Name";
            ddlFlightArrivals.DataSource = dv;
            ddlFlightArrivals.DataBind();
            if (Type == 1)
            {
                ddlFlightArrivals.Items.Insert(0, new ListItem("--- Chọn lượt bay ---", "-1"));
            }
            else
            {
                ddlFlightArrivals.Items.Insert(0, new ListItem("--- Chọn lượt đi ---", "-1"));
            }
        }

        private void LoadTicketByConcern()
        {
            ddlTicket.DataSource = new TicketController().TicketListByConcern(_priceInfo.ItemType, _ticketInfo.ConcernId).Where(p => p.Status == (int)Globals.DocumentStatus.Use).OrderBy(p => p.PriceName);
            ddlTicket.DataTextField = "Pricename";
            ddlTicket.DataValueField = "TicketId";
            ddlTicket.DataBind();

            ddlTicket.Items.Insert(0, new ListItem("--- Chọn vé---", "-1"));
        }
        private void LoadInsuranceByPartner()
        {
            ddlTicket.DataSource = new InsuranceController().ListInsuranceByPartner(_insuranceInfo.PartnerId).Where(p => p.Status == (int)Globals.DocumentStatus.Use).OrderBy(p => p.InsuranceName);
            ddlTicket.DataTextField = "InsuranceName";
            ddlTicket.DataValueField = "InsuranceId";
            ddlTicket.DataBind();

            ddlTicket.Items.Insert(0, new ListItem("--- Chọn bảo hiểm---", "-1"));
        }
        private void LoadServiceByLocation()
        {
            ddlTicket.DataSource = new ServiceController().ListServiceByPartnerAndLocation(_serviceInfo.PartnerId,_serviceInfo.LocationId).Where(p => p.Status == (int)Globals.DocumentStatus.Use).OrderBy(p => p.ServiceName);
            ddlTicket.DataTextField = "ServiceName";
            ddlTicket.DataValueField = "ServiceId";
            ddlTicket.DataBind();

            ddlTicket.Items.Insert(0, new ListItem("--- Chọn dịch vụ---", "-1"));
        }
        public void LoadRoomByHotel()
        {
            ddlTicket.DataSource =
                new RoomController().ListRoomByHotel(_roomInfo.HotelId)
                    .Where(p => p.Status == (int) Globals.DocumentStatus.Use)
                    .OrderBy(p => p.RoomName);

            ddlTicket.DataTextField = "RoomName";
            ddlTicket.DataValueField = "RoomId";
            ddlTicket.DataBind();

            ddlTicket.Items.Insert(0, new ListItem("--- Chọn loại phòng ---", "-1"));
        }

        public void LoadMenuByRestaurantAndMenuType()
        {
            ddlTicket.DataSource =
                new MenuController().ListMenuByRestaurantAndMenuType(_menuInfo.RestaurantId,_menuInfo.MenuTypeId)
                    .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                    .OrderBy(p => p.MenuName);

            ddlTicket.DataTextField = "MenuName";
            ddlTicket.DataValueField = "MenuId";
            ddlTicket.DataBind();

            ddlTicket.Items.Insert(0, new ListItem("--- Chọn thực đơn ---", "-1"));
        }
    }
}